/**
 * 加加权限菜单
 * @param reourses
 */
import {Tool} from "@/utils/tool";
import {IS_SHOW_MENU, ROURCE_TYPE} from "@/utils/enums";

export const getmenuBar = (reourses) => {
    let resources = Tool.getInstance().getLoginUser().resources
    let item = []
    if(resources==undefined){
        resources=[];
    }
    return getBarTree(item, resources);
}

function getBarTree(item, resources) {
    for (let i = 0; i < resources.length; i++) {
        //  如果不是按钮
        if (resources[i].category != ROURCE_TYPE.BUTTON.key&&resources[i].isShow==IS_SHOW_MENU.SHOW.key) {
            if (resources[i].parent == '' && resources[i]["children"] == null) {
                item.push(
                    {
                        "index":resources[i].path,
                        "title":resources[i].name,
                        "page":resources[i].page,
                        "icon":resources[i].icon,
                         "linkType":resources[i].linkType
                    }
                )
            } else if (resources[i].parent == '' && resources[i]["children"] != null&&resources[i].isShow==IS_SHOW_MENU.SHOW.key) {
                if (resources[i].path == null) {
                    item.push(
                        {
                            "index":resources[i].page,
                            "title":resources[i].name,
                            "page":resources[i].page,
                            "icon":resources[i].icon,
                            "linkType":resources[i].linkType
                        }
                    )
                } else {
                    if(resources[i].isShow==IS_SHOW_MENU.SHOW.key) {
                        item.push(
                            {
                                "index": resources[i].path,
                                "title": resources[i].name,
                                "page":resources[i].page,
                                "icon": resources[i].icon,
                                "linkType":resources[i].linkType
                            }
                        )
                    }
                }
            }
            if (resources[i]["children"] != null) {
                for (let j = 0; j < resources[i].children.length; j++) {
                    //判断是否要创建子菜单
                    if (resources[i].children[j].category != ROURCE_TYPE.BUTTON.key&&resources[i].children[j].isShow==IS_SHOW_MENU.SHOW.key) {
                        item[item.length-1].subs = []
                    }
                }
                // for (let j = 0; j < resources[i].children.length; j++) {
                //     //过滤按钮
                //     if (resources[i].children[j].category != ROURCE_TYPE.BUTTON.key &&resources[i].children[j].isShow==IS_SHOW_MENU.SHOW.key) {
                //         item[i].subs.push({index:'',title:'',icon:''})
                //
                //     }
                // }
                for (let j = 0; j < resources[i].children.length; j++) {
                    //过滤按钮,和隐藏的菜单
                    if (resources[i].children[j].category != ROURCE_TYPE.BUTTON.key&&resources[i].children[j].isShow==IS_SHOW_MENU.SHOW.key ) {
                        item[item.length-1].subs.push({
                            "index":resources[i].children[j].path,
                            "title":resources[i].children[j].name,
                            "page":resources[i].children[j].page,
                            "icon":resources[i].children[j].icon,
                            "linkType":resources[i].children[j].linkType
                        }) ;
                    }

                }
                if (item[item.length-1]!=undefined&&item[item.length-1].subs != undefined) {
                    getBarTree(item[item.length-1].subs, resources[i].children)
                }
            }
        }

    }
    return item.filter(value => Object.keys(value).length !== 0);
}
function gettopBarTree(item, resources) {
    for (let i = 0; i < resources.length; i++) {
        //  如果不是按钮
        if (resources[i].category != ROURCE_TYPE.BUTTON.key && resources[i].isShow == IS_SHOW_MENU.SHOW.key) {
            if (resources[i].parent == '') {
                item.push(
                    {
                        "index": resources[i].path,
                        "title": resources[i].name,
                        "icon": resources[i].icon,
                        "linkType":resources[i].linkType
                    }
                )
            }

        }

    }
    return item;
}


